package com.huike.game.Activity;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.huike.game.R;
import com.huike.game.base.Game8Table;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class GameActivity8 extends AppCompatActivity {

    private ImageView ivLeftImage;
    private TextView tvLeftText;
    private TextView tvContentTitle;
    private ImageView ivRigthImage;
    private TextView tvRightText;
    private ListView lvGame8Table;
    private ArrayList<Game8Table> list;
    private Spinner sp_game8;
    private Button bt_game8_query;
    private TableAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game8);

        list = new ArrayList<>();
        list.add(new Game8Table("1","9","9","9"));
        list.add(new Game8Table("2","8","8","8"));
        list.add(new Game8Table("3","7","7","7"));
        list.add(new Game8Table("4","6","6","6"));

    //  初始化id
        initIds();
         adapter = new TableAdapter();
        lvGame8Table.setAdapter(adapter);

//        Spinner 监听
        sp_game8.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            //当选中某一个数据项时触发该方法
            /*
             * parent接收的是被选择的数据项所属的 Spinner对象，
             * view参数接收的是显示被选择的数据项的TextView对象
             * position接收的是被选择的数据项在适配器中的位置
             * id被选择的数据项的行号
             */
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, final int position, long id) {
                bt_game8_query.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        switch (position){
                            case 0://路口升序
                                Collections.sort(list, new Comparator<Game8Table>() {
                                    @Override
                                    public int compare(Game8Table g1, Game8Table t1) {
                                        return Integer.parseInt(g1.getCross()) - Integer.parseInt(t1.getCross());
                                    }
                                });
                                adapter.notifyDataSetChanged();
                                break;
                            case 1: //路口降序
                                Collections.sort(list, new Comparator<Game8Table>() {
                                    @Override
                                    public int compare(Game8Table g1, Game8Table t1) {
                                        return Integer.parseInt(g1.getCross()) - Integer.parseInt(t1.getCross());
                                    }
                                });
                                Collections.reverse(list);
                                adapter.notifyDataSetChanged();
                                break;
                            case 2://红灯升序
                                Collections.sort(list, new Comparator<Game8Table>() {
                                    @Override
                                    public int compare(Game8Table g1, Game8Table t1) {
                                        return Integer.parseInt(g1.getRed()) - Integer.parseInt(t1.getRed());
                                    }
                                });
                                adapter.notifyDataSetChanged();
                                break;
                            case 3://红灯降序
                                Collections.sort(list, new Comparator<Game8Table>() {
                                    @Override
                                    public int compare(Game8Table g1, Game8Table t1) {
                                        return Integer.parseInt(g1.getRed()) - Integer.parseInt(t1.getRed());
                                    }
                                });
                                Collections.reverse(list);
                                adapter.notifyDataSetChanged();
                                break;
                            case 4://绿灯升序
                                Collections.sort(list, new Comparator<Game8Table>() {
                                    @Override
                                    public int compare(Game8Table g1, Game8Table t1) {
                                        return Integer.parseInt(g1.getGreen()) - Integer.parseInt(t1.getGreen());
                                    }
                                });
                                adapter.notifyDataSetChanged();
                                break;
                            case 5://绿等降序
                                Collections.sort(list, new Comparator<Game8Table>() {
                                    @Override
                                    public int compare(Game8Table g1, Game8Table t1) {
                                        return Integer.parseInt(g1.getGreen()) - Integer.parseInt(t1.getGreen());
                                    }
                                });
                                Collections.reverse(list);
                                adapter.notifyDataSetChanged();
                                break;
                            case 6://黄灯升序
                                Collections.sort(list, new Comparator<Game8Table>() {
                                    @Override
                                    public int compare(Game8Table g1, Game8Table t1) {
                                        return Integer.parseInt(g1.getYellow()) - Integer.parseInt(t1.getYellow());
                                    }
                                });
                                adapter.notifyDataSetChanged();
                                break;
                            case 7://黄灯降序
                                Collections.sort(list, new Comparator<Game8Table>() {
                                    @Override
                                    public int compare(Game8Table g1, Game8Table t1) {
                                        return Integer.parseInt(g1.getYellow()) - Integer.parseInt(t1.getYellow());
                                    }
                                });
                                Collections.reverse(list);//数组反转
                                adapter.notifyDataSetChanged();
                                break;
                        }
                    }
                });
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


    }

    private void initIds() {
        ivLeftImage = (ImageView) findViewById(R.id.iv_left_image);
        tvLeftText = (TextView) findViewById(R.id.tv_left_text);
        tvContentTitle = (TextView) findViewById(R.id.tv_content_title);
        ivRigthImage = (ImageView) findViewById(R.id.iv_rigth_image);
        tvRightText = (TextView) findViewById(R.id.tv_right_text);
        lvGame8Table = (ListView) findViewById(R.id.lv_game8_table);
        sp_game8 =  findViewById(R.id.sp_game8);
        bt_game8_query =  findViewById(R.id.bt_game8_query);


        tvRightText.setVisibility(View.GONE);
        ivRigthImage.setVisibility(View.GONE);
        tvContentTitle.setText("红绿灯管理");
        ivLeftImage.setImageResource(R.drawable.tiao);
        tvRightText.setVisibility(View.GONE);
    }

    class TableAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View Cview, ViewGroup viewGroup) {

            ViewHoder hoder = null;

            if(Cview == null){
                hoder = new ViewHoder();

                Cview = View.inflate(GameActivity8.this,R.layout.game8_table_llv,null);
                hoder.tvGame8Cross = Cview.findViewById(R.id.tv_game8_cross) ;
                hoder.tvGame8Red = Cview.findViewById(R.id.tv_game8_red) ;
                hoder.tvGame8Yellow = Cview.findViewById(R.id.tv_game8_yellow) ;
                hoder.tvGame8Green = Cview.findViewById(R.id.tv_game8_green) ;
                Cview.setTag(hoder);
            }else{
                hoder = (ViewHoder) Cview.getTag();
            }

            Game8Table table = list.get(i);
                hoder.tvGame8Cross.setText(table.getCross());
                hoder.tvGame8Red.setText(table.getRed());
                hoder.tvGame8Yellow.setText(table.getYellow());
                hoder.tvGame8Green.setText(table.getGreen());

            return Cview;
        }
    }

    class ViewHoder{
        private TextView tvGame8Cross;
        private TextView tvGame8Red;
        private TextView tvGame8Yellow;
        private TextView tvGame8Green;
    }

}
